package nl.weeaboo.sh.objects;

import java.awt.Rectangle;

import nl.weeaboo.sh.level.ActiveTiles;
import nl.weeaboo.sh.level.Level;
import nl.weeaboo.sh.level.Tile;
import nl.weeaboo.sh.model.DamageRect;

public class CollisionUtil {

	//Functions
	public static void collideSprites(Level l, Collidable c) {
		Rectangle a = new Rectangle();
		Rectangle b = new Rectangle();
		
		Sprite s1 = c.getSprite();
		for (Sprite s2 : l.getSprites()) {			
			for (int n = 0; n < 4; n++) {
				Rectangle rects1[];
				Rectangle rects2[];
				
				if (n == 0) {
					rects1 = s1.getDamageRects();
					rects2 = s2.getHitRects();
					if (!c.getCollidesDamage(s2)) continue;
				} else if (n == 1) {
					rects1 = s1.getHitRects();
					rects2 = s2.getDamageRects();
					if (!c.getCollidesDamage(s2)) continue;
				} else if (n == 2) {
					rects1 = s1.getSplashRects();
					rects2 = s2.getHitRects();
					if (!c.getCollidesSplash(s2)) continue;
				} else {
					rects1 = s1.getHitRects();
					rects2 = s2.getSplashRects();
					if (!c.getCollidesSplash(s2)) continue;
				}

				for (Rectangle r1 : rects1) {
					a.x = s1.x + r1.x;
					a.y = s1.y + r1.y;
					a.width = r1.width;
					a.height = r1.height;
					for (Rectangle r2 : rects2) {
						b.x = s2.x + r2.x;
						b.y = s2.y + r2.y;
						b.width = r2.width;
						b.height = r2.height;						
						if (a.intersects(b)) {
							Rectangle overlap = a.intersection(b);
							if (n == 0) {
								DamageRect dr = (DamageRect)r1;
								c.onCollision(s1, s2, overlap, Math.round(s1.getPower() * dr.damage), dr);
							} else if (n == 1) {
								DamageRect dr = (DamageRect)r2;
								c.onCollision(s2, s1, overlap, Math.round(s2.getPower() * dr.damage), dr);
							} else if (n == 2) {
								DamageRect dr = (DamageRect)r1;
								c.onSplashCollision(s1, s2, overlap, Math.round(s1.getPower() * dr.damage), dr);
							} else if (n == 3) {
								DamageRect dr = (DamageRect)r2;
								c.onSplashCollision(s2, s1, overlap, Math.round(s2.getPower() * dr.damage), dr);
							}
						}
					}				
				}
			}
		}
	}
	
	public static void collideTiles(Level level, Collidable c) {
		ActiveTiles at = level.getActiveTiles();
		Tile tiles[][] = at.getTiles();
		int atx = at.getX();
		int aty = at.getY();
		
		Sprite s1 = c.getSprite();
		Rectangle a = new Rectangle();
		for (int n = 0; n < 3; n++) {
			Rectangle rects[];
			if (n == 0) rects = s1.getDamageRects();
			else if (n == 1) rects = s1.getSplashRects();
			else rects = s1.getHitRects();
			
			if (n == 1) continue; //Ignore splash damage
			
			for (Rectangle r : rects) {
				int minX = Level.toTileCoord(a.x = s1.x + r.x);
				int maxX = Level.toTileCoord(s1.x + r.x + (a.width = r.width));
				int minY = Level.toTileCoord(a.y = s1.y + r.y);
				int maxY = Level.toTileCoord(s1.y + r.y + (a.height = r.height));
				
				for (int y = minY; y <= maxY; y++) {
					for (int x = minX; x <= maxX; x++) {
						if (x >= atx && x < atx+tiles.length && y >= aty && y < aty+tiles[x-atx].length) {
							Tile tile = tiles[x-atx][y-aty];
							if (tile != null) {
								Rectangle overlap = a.intersection(new Rectangle(x*Level.tileSize, y*Level.tileSize, Level.tileSize, Level.tileSize));
								if (n == 0) {
									DamageRect dr = (DamageRect)r;
									if (tile.getHp() > 0) c.onCollision(s1, tile, overlap, Math.round(s1.getPower() * dr.damage)); 
								} else if (n == 1) {
									DamageRect dr = (DamageRect)r;
									if (tile.getHp() > 0) c.onCollision(s1, tile, overlap, Math.round(s1.getPower() * dr.damage)); 
								} else if (n == 2) {
									if (tile.getDamage() > 0) c.onCollision(s1, tile, overlap, tile.getDamage()); 
								}
							}
						}
					}
				}
			}
		}
	}
	
	//Getters
	
	//Setters
	
}
